package com.jinunn.yygh.controller;

import com.atguigu.yygh.model.cmn.Dict;
import com.jinunn.common.result.Result;
import com.jinunn.yygh.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : JinDun
 * @date : 2021/4/24 22:47
 */
@Api(value = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation("根据id查询子数据列表")
    @GetMapping("findChileData/{id}")
    public Result<List<Dict>> findChileData(@PathVariable("id") Long id){
        List<Dict> list =dictService.findChileData(id);
        return Result.ok(list);
    }

    @ApiOperation("导出数据字典接口")
    @GetMapping("exportDate")
    public void exportDate(HttpServletResponse response){
        dictService.exportData(response);
    }

    @ApiOperation("读取数据字典接口")
    @PostMapping("importDate")
    public Result<Dict> importDate(MultipartFile file){
        dictService.importData(file);
        return Result.ok();
    }

    @ApiOperation("根据dictcode和value查询")
    @GetMapping("getName/{dictCode}/{value}")
    public String getDictName(@PathVariable("dictCode") String dictCode,
                              @PathVariable("value") String value){
        return dictService.getDictName(dictCode,value);
    }

    @ApiOperation("根据value查询")
    @GetMapping("getName/{value}")
    public String getDictName(@PathVariable("value") String value){
        return dictService.getDictName("",value);
    }
}
